<?php

require_once(sfConfig::get('sf_lib_dir').'/filter/doctrine/BaseFormFilterDoctrine.class.php');

/**
 * Item filter form base class.
 *
 * @package    filters
 * @subpackage Item *
 * @version    SVN: $Id: sfDoctrineFormFilterGeneratedTemplate.php 11675 2008-09-19 15:21:38Z fabien $
 */
class BaseItemFormFilter extends BaseFormFilterDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id_tipo'           => new sfWidgetFormDoctrineChoice(array('model' => 'Tipo', 'add_empty' => true)),
      'id_item_pai'       => new sfWidgetFormDoctrineChoice(array('model' => 'Item', 'add_empty' => true)),
      'user_id'           => new sfWidgetFormDoctrineChoice(array('model' => 'sfGuardUserProfile', 'add_empty' => true)),
      'nm_item'           => new sfWidgetFormFilterInput(),
      'st_item'           => new sfWidgetFormFilterInput(),
      'is_top'            => new sfWidgetFormFilterInput(),
      'ds_item'           => new sfWidgetFormFilterInput(),
      'ds_resumo_item'    => new sfWidgetFormFilterInput(),
      'ds_tags'           => new sfWidgetFormFilterInput(),
      'ds_url'            => new sfWidgetFormFilterInput(),
      'id_artigo_conexao' => new sfWidgetFormFilterInput(),
      'slug'              => new sfWidgetFormFilterInput(),
      'created_at'        => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => true)),
      'updated_at'        => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => true)),
      'deleted'           => new sfWidgetFormChoice(array('choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no'))),
      'categoria_list'    => new sfWidgetFormDoctrineChoiceMany(array('model' => 'Categoria')),
    ));

    $this->setValidators(array(
      'id_tipo'           => new sfValidatorDoctrineChoice(array('required' => false, 'model' => 'Tipo', 'column' => 'id_tipo')),
      'id_item_pai'       => new sfValidatorDoctrineChoice(array('required' => false, 'model' => 'Item', 'column' => 'id_item')),
      'user_id'           => new sfValidatorDoctrineChoice(array('required' => false, 'model' => 'sfGuardUserProfile', 'column' => 'user_id')),
      'nm_item'           => new sfValidatorPass(array('required' => false)),
      'st_item'           => new sfValidatorPass(array('required' => false)),
      'is_top'            => new sfValidatorPass(array('required' => false)),
      'ds_item'           => new sfValidatorPass(array('required' => false)),
      'ds_resumo_item'    => new sfValidatorPass(array('required' => false)),
      'ds_tags'           => new sfValidatorPass(array('required' => false)),
      'ds_url'            => new sfValidatorPass(array('required' => false)),
      'id_artigo_conexao' => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'slug'              => new sfValidatorPass(array('required' => false)),
      'created_at'        => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))),
      'updated_at'        => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))),
      'deleted'           => new sfValidatorChoice(array('required' => false, 'choices' => array('', 1, 0))),
      'categoria_list'    => new sfValidatorDoctrineChoiceMany(array('model' => 'Categoria', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('item_filters[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function addCategoriaListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query->leftJoin('r.ItemCategoria ItemCategoria')
          ->andWhereIn('ItemCategoria.id_categoria', $values);
  }

  public function getModelName()
  {
    return 'Item';
  }

  public function getFields()
  {
    return array(
      'id_item'           => 'Number',
      'id_tipo'           => 'ForeignKey',
      'id_item_pai'       => 'ForeignKey',
      'user_id'           => 'ForeignKey',
      'nm_item'           => 'Text',
      'st_item'           => 'Text',
      'is_top'            => 'Text',
      'ds_item'           => 'Text',
      'ds_resumo_item'    => 'Text',
      'ds_tags'           => 'Text',
      'ds_url'            => 'Text',
      'id_artigo_conexao' => 'Number',
      'slug'              => 'Text',
      'created_at'        => 'Date',
      'updated_at'        => 'Date',
      'deleted'           => 'Boolean',
      'categoria_list'    => 'ManyKey',
    );
  }
}